#include "VolumetricMeshLoader.h"
#include <stdio.h>

VolumetricMeshLoader::VolumetricMeshLoader(void)
{
}

VolumetricMeshLoader::~VolumetricMeshLoader(void)
{
}

void VolumetricMeshLoader::LoadTetMesh( ElementContainer* container, char* name )
{
	FILE* fp;
	float px, py, pz;
	int dump, v0, v1, v2, v3;

	fp = fopen(name, "r");

	fscanf(fp,"*NODE\n");

	fscanf(fp,"%d\n",&(container->vertNum));

	for (int i=0;i<container->vertNum;i++)
	{
		fscanf(fp,"%d, %f, %f, %f\n",&dump,&px,&py,&pz);

		Vertex* vert = new Vertex;
		vert->position = vec3f(px,py,pz);
		vert->origin = vert->position;
		vert->velocity = vec3f(0,0,0);
		vert->force = vec3f(0,0,0);
		vert->fext = vec3f(0,0,0);
		vert->index = i;
		vert->mass = 0.0f;

		container->vertices.push_back(vert);
	}

	fscanf(fp,"*ELEMENT\n");

	fscanf(fp,"%d\n",&(container->tetNum));

	for (int i=0;i<container->tetNum;i++)
	{
		fscanf(fp,"%d, %d, %d, %d, %d\n",&dump,&v0,&v1,&v2,&v3);

		Tet* t = new Tet;

		Vertex *va, *vb, *vc, *vd;

		va = container->vertices.at(v0-1);
		vb = container->vertices.at(v1-1);
		vc = container->vertices.at(v2-1);
		vd = container->vertices.at(v3-1);

		////////////////////////////////////////////////////////////////////////// Vertex Mass

		t->vertices[0] = va;
		t->vertices[1] = vb;
		t->vertices[2] = vc;
		t->vertices[3] = vd;

		t->index = i;

		container->tets.push_back(t);
	}

	fclose(fp);

}